/*
 * Aboboo Android
 * Copyright (C) 2012 www.aboboo.com
 * 
 */

package com.aboboo.android.player;

import com.aboboo.android.R;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

/**
 * 
 * @author fangpeng
 * 
 * @version 1.0 2013-01-31
 * 
 */
public class SelectFileActivity extends ListActivity {
	public static final String KEY_FILE_URL_ID = "Url";
	public static final String KEY_FILE_TITLE_ID = "Title";

	private Cursor mCursor;
	private ListView mFilelist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mCursor = getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		// 初始化歌曲列表
		mFilelist = this.getListView();

		startManagingCursor(mCursor);

		String[] from = new String[] { MediaStore.Audio.Media.TITLE };
		int[] to = new int[] { R.id.music_title };

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.file_list_row, mCursor, from, to);

		mFilelist.setAdapter(adapter);
		mFilelist.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mCursor.moveToPosition(position);
				Intent i = new Intent(SelectFileActivity.this,
						SelectFileActivity.class);
				String url = mCursor.getString(mCursor
						.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
				i.putExtra(KEY_FILE_URL_ID, url);
				String title = mCursor.getString(mCursor
						.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
				i.putExtra(KEY_FILE_TITLE_ID, title);
				SelectFileActivity.this.setResult(RESULT_OK, i);
				SelectFileActivity.this.finish();
			}
		});
	}

}
